<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/10/22
 * Time: 15:33
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\common\lib;

class Show
{
    /**
     * @param int $code
     * @param string $msg
     * @param array $data
     * @param int $count
     * @return \think\response\Json
     */
    public static function json_show(int $code = 0, string $msg = '', array $data = [], int $count = 0)
    {
        $result = [
            "code" => $code,
            "msg" => $msg,
            "data" => $data,
            "count" => $count
        ];
        return json($result);
    }

    /**
     * @param array $data
     * @return false|string
     */
    public static function json_encode(array $data = [])
    {
        return json_encode($data, JSON_UNESCAPED_UNICODE);
    }

    /**
     * @param string $string
     * @return mixed
     */
    public static function json_decode(string $string = '')
    {
        return json_decode($string, true);
    }

    /**
     * 计算两点之间的距离
     * @param $lng1 经度1
     * @param $lat1 纬度1
     * @param $lng2 经度2
     * @param $lat2 纬度2
     * @param int $unit 1 m，2 km
     * @param int $decimal 位数
     * @return float
     */
    public static function getDistance($lng1, $lat1, $lng2, $lat2, $unit = 2, $decimal = 2)
    {

        $EARTH_RADIUS = 6370.996; // 地球半径系数
        $PI = 3.1415926535898;

        $radLat1 = $lat1 * $PI / 180.0;
        $radLat2 = $lat2 * $PI / 180.0;

        $radLng1 = $lng1 * $PI / 180.0;
        $radLng2 = $lng2 * $PI / 180.0;

        $a = $radLat1 - $radLat2;
        $b = $radLng1 - $radLng2;

        $distance = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
        $distance = $distance * $EARTH_RADIUS * 1000;

        if ($unit === 2) {
            $distance /= 1000;
        }

        return round($distance, $decimal);
    }

    /**
     * @param $lat
     * @param $lng
     * @param int $kilometers
     * @return \float[][]|\int[][]
     */
    public static function getKilometers($lat, $lng, $kilometers = 5)
    {
        $PI = 3.1415926535898;
        $EARTH_RADIUS = 6370.996;
        $range = 180 / $PI / $EARTH_RADIUS * $kilometers; //里面的 $kilometers 就代表搜索 km 之内，单位km
        $lngR = $range / cos($lat * $PI / 180);
        $maxLat = $lat + $range; //最大纬度
        $minLat = $lat - $range; //最小纬度
        $maxLng = $lng + $lngR; //最大经度
        $minLng = $lng - $lngR; //最小经度
        return [
            "min" => [
                $minLat,
                $maxLat
            ],
            "max" => [
                $minLng,
                $maxLng
            ]
        ];
    }

    /**
     * 获取今天周几
     * @param $value
     * @return string
     */
    public static function getTodayWeek($value)
    {

        $array = ["周天", "周一", "周二", "周三", "周四", "周五", "周六"];
        return $array[$value];
    }

    /**
     * @param $array
     * @return array
     */
    public static function getArraySeat($array)
    {
        $lists = [];
        foreach ($array as $k => $v) {
            if (isset($lists[$v['seatY']])) {
                array_push($lists[$v['seatY']]['x'], [
                    "key" => $v['seatX'],
                    "dis" => $v['is_disabled'],
                    "money"=>$v['money'],
                    "state"=>$v['state']
                ]);
            } else {
                $lists[$v['seatY']] = [
                    "Y" => $v['seatY'],
                    "x" => [
                        [
                            "key" => $v['seatX'],
                            "dis" => $v['is_disabled'],
                            "money"=>$v['money'],
                            "state"=>$v['state']
                        ]
                    ]
                ];
            }
        }
        return $lists;
    }

    /**
     * @param array $array
     * @return array
     */
    public static function getArrayList(array $array = [])
    {
        $lists = [];
        foreach ($array as $k => $v) {
            $lists['all'][] = [$v['seatX'], $v['seatY'],$v['money'],$v['is_disabled'],$v['state']];
//            if ($v['is_disabled'] == 1) {
//                $lists['dis'][] = [$v['seatX'], $v['seatY']];
//            }
        }
        return $lists;
    }
}